<template>
  <a-card>
    <div class="totalView">
      <!--      左-->
      <div class="left">
        <head class="left-header">
          <span></span>
          <span class="left-header-text">产品层级</span>
          <span></span>
        </head>
        <a-input-search style="margin-bottom: 8px" placeholder="请输入层级名称" enter-button @search="onSearch" />
        <a-tree
          :auto-expand-parent="autoExpandParent"
          :tree-data="treeData"
          @expand="onExpand"
          :checkedKeys="checkedKeys"
          :checkStrictly="true"
          :replaceFields="{children: 'children', title: 'typeName', key: 'typeCode'}"
          style="width:100%;border: 0px;border: none;outline:none;"
          @select="selectNode"
        >
        </a-tree>
      </div>
      <!--      右-->
      <div class="right">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="8">
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="">
                  <a-input placeholder="请输入商品编号" v-model="queryParam.productCode"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="">
                  <a-input placeholder="请输入商品名称" v-model="queryParam.productName"></a-input>
                </a-form-item>
              </a-col>
              <template v-if="toggleSearchStatus">
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <a-form-item label="">
                    <a-input v-model="queryParam.custCode" placeholder="请输入经销商编号"></a-input>
                  </a-form-item>
                </a-col>
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <a-form-item label="">
                    <a-input v-model="queryParam.custName" placeholder="请输入经销商名称"></a-input>
                  </a-form-item>
                </a-col>
              </template>
              <!--          <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
              <!--            <a-form-item label="">-->
              <!--              <cn-dict-select-tag placeholder="请选择门店等级" dict-code="rule_type" v-model="queryParam.terminalLevel"/>-->
              <!--            </a-form-item>-->
              <!--          </a-col>-->
              <!--          <template v-if="toggleSearchStatus">-->
              <!--            <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
              <!--              <a-form-item label="">-->
              <!--                <a-input placeholder="请输入经销商编号" v-model="queryParam.custName"></a-input>-->
              <!--              </a-form-item>-->
              <!--            </a-col>-->
              <!--          </template>-->
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                            <a @click="handleToggleSearch" style="margin-left: 8px">
                              {{ toggleSearchStatus ? '收起' : '展开' }}
                              <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                            </a>
            </a-col>
          </span>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button @click="handleButon('newAdd')" type="primary">新增</a-button>
          <a-button type="primary" icon="start" @click="startorstop('009')">启用</a-button>
          <a-button type="primary"  @click="startorstop('003')">停用</a-button>
          <!--      <a-button @click="handleEnable" type="primary">启用</a-button>-->
          <!--      <a-button @click="handleDisable" type="primary">停用</a-button>-->
          <!--      <a-button @click="handleTerminal" type="primary">关联终端</a-button>-->

          <a-button type="primary" icon="download" @click="downExportXls('dmsProductPrice_service')">导出</a-button>
          <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
          <!--        <a-button type="primary" icon="import">导入</a-button>-->
          <!--      </a-upload>-->
          <!-- 高级查询区域 -->
          <!--      <j-super-query :fieldList="fieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
          <!--      <a-dropdown v-if="selectedRowKeys.length > 0">-->
          <!--        <a-menu slot="overlay">-->
          <!--          <a-menu-item key="1" @click="batchDel">-->
          <!--            <a-icon type="delete"/>-->
          <!--            删除-->
          <!--          </a-menu-item>-->
          <!--        </a-menu>-->
          <!--        <a-button style="margin-left: 8px"> 批量操作-->
          <!--          <a-icon type="down"/>-->
          <!--        </a-button>-->
          <!--      </a-dropdown>-->
        </div>

        <!-- table区域-begin -->
        <div>
<!--          <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
<!--            <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{-->
<!--              selectedRowKeys.length-->
<!--            }}</a>项-->
<!--            <a style="margin-left: 24px" @click="onClearSelected">清空</a>-->
<!--            <span style="float:right;">-->
<!--          <a @click="loadData()"><a-icon type="sync" />刷新</a>-->
<!--          <a-divider type="vertical" />-->
<!--          <a-popover title="自定义列" trigger="click" placement="leftBottom">-->
<!--            <template slot="content">-->
<!--              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">-->
<!--                <a-row>-->
<!--                  <template v-for="(item,index) in defColumns">-->
<!--                    <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">-->
<!--                        <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>-->
<!--                    </template>-->
<!--                  </template>-->
<!--                </a-row>-->
<!--              </a-checkbox-group>-->
<!--            </template>-->
<!--            <a><a-icon type="setting" />设置</a>-->
<!--          </a-popover>-->
<!--        </span>-->
<!--          </div>-->

          <a-table
            ref="table"
            size="middle"
            class='Table'
            bordered
            rowKey="id"
            :scroll="{
x:'max-content'
        }"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            :customRow="onClickRow"
            :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
            @change="handleTableChange">


            <!--        <template slot="avatar" slot-scope="text, record, index">-->
            <!--          <div class="anty-img-wrap">-->
            <!--            <a-avatar shape="square" :src="record.avatar" icon="user"/>-->
            <!--          </div>-->
            <!--        </template>-->
            <!--        <div slot="isActiveStatus" slot-scope="text,recode">-->
            <!--          {{ text|isActiveStatus }}-->
            <!--        </div>-->
            <!--        <div slot="filterCustType" slot-scope="text,recode">-->
            <!--        {{ text|filterCustType }}-->
            <!--    </div>-->
            <!--        设置图标-->
            <!--        <a-icon slot="filterIcon" type='setting' :style="{ fontSize:'16px',color:  '#108ee9' }" />-->
            <div slot="productName" slot-scope="text,record">
              <a @click="handleButon('detail',record)">{{ text }}</a>
            </div>

            <div slot="filterDropdown">
              <a-card>
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row>
                    <template v-for="(item,index) in defColumns">
                      <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                        <a-col :span="12">
                          <a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox>
                        </a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </a-card>
            </div>
            <span slot="action" slot-scope="text, record">
          <a @click="handleButon('edit',record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">

              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
          </a-table>
        </div>
      </div>
    </div>
    <popup ref="popupModal" @ok="modalFormOk" v-bind="addModalData"></popup>
  </a-card>
</template>

<script>
import { getAction,postAction } from '@/api/manage'
import { PageListMixin } from '@/mixins/PageListMixin'
import popup from '@/components/view/base-crud/form/FormAddEdit'
import Vue from 'vue'

export default {
  name: 'productHierarchy',
  description: '产品层级',
  mixins: [PageListMixin],
  components:{
    popup,
  },
  data() {
    return {
      modaltitle: '',
      urlAdd: {
        addOrUpdateUrl: window._CONFIG['domianURL'] + '/cnProductTypeController/saveOrUpdateEntity'
      },
      addForm: {
        list: [
          // 输入框
          {
            span: 0,
            md: 12,
            label: '层级名称',
            type: 'input',
            placeholder: '请输入',
            name: 'terminalCode',
            check: {
              rules: [{ required: true, message: '请输入门店编号' }]
            }
          },
          {
            span: 0,
            md: 12,
            label: '层级名称',
            type: 'j-select',
            placeholder: '请选择',
            dictCode:'levelType',
            name: 'productlevel',
            check: {
              rules: [{ required: true, message: '请选择层级类型' }]
            }
          },
          {
            span: 0,
            md: 12,
            label: '上级名称',
            type: 'j-select',
            placeholder: '请选择',
            dictCode:'levelType',
            name: 'productlevel',
            check: {
              rules: [{ required: true, message: '请选择上级' }]
            }
          },
          {
            span: 0,
            md: 24,
            label: '备注',
            type: 'input',
            inputType:'textarea',
            placeholder: '请输入',
            name: 'terminalCode',
            labelCol: {
              xs: { span: 24 },
              sm: { span: 3 }
            },
            wrapperCol: {
              xs: { span: 24 },
              sm: { span: 20 }
            },
            check: {
              rules: [{ required: true, message: '请输入备注' }]
            }
          },
        ]
      },
      checkedKeys:[],
      treeData:[],
      autoExpandParent:false,
      queryParam: {},
      //列设置
      settingColumns: [],
      // 列定义
      defColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '商品编号',
          align: 'left',
          dataIndex: 'productCode'
        },
        {
          title: '商品名称',
          align: 'left',
          dataIndex: 'productName',
          scopedSlots: { customRender: 'productName' }
        },
        {
          title: '经销商编号',
          align: 'left',
          dataIndex: 'custCode'
        },
        {
          title: '经销商名称',
          align: 'left',
          dataIndex: 'custName'
        },
        // {
        //   title: '价格组',
        //   align: 'left',
        //   dataIndex: 'groupPrice'
        // },
        {
          title: '渠道名称',
          align: 'left',
          dataIndex: 'channelName'
        },
        {
          title: '价格',
          align: 'left',
          dataIndex: 'price'
        },
        {
          title: '开始时间',
          align: 'left',
          dataIndex: 'startTime'
        },
        {
          title: '结束时间',
          align: 'left',
          dataIndex: 'endTime'
        },
        // {
        //   title: '价格单位编号',
        //   align: 'left',
        //   dataIndex: 'unitCode'
        // },
        {
          title: '价格单位名称',
          align: 'left',
          dataIndex: 'unitName'
        },
        // {
        //   title: 'erp凭证码',
        //   align: 'left',
        //   dataIndex: 'erpKey'
        // },
        {
          title: '创建时间',
          align: 'left',
          dataIndex: 'createDate',
          customRender: (t, r, dex) => {
            return r.createDate + r.createDateTime
          }
        },
        {
          title: '最近更新人',
          align: 'left',
          dataIndex: 'updateName',
          ellipsis: true
        },
        {
          title: '最近更新时间',
          align: 'left',
          dataIndex: 'updateDate'
        },
        {
          title: '状态',
          align: 'left',
          dataIndex: 'status_dictText'
        },
        {
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
          align: 'center',
          width: 150,
          scopedSlots: {
            filterDropdown: 'filterDropdown',
            // filterIcon: 'filterIcon',
            customRender: 'action'
          }
        }
      ],
      //表头
      columns: [],
      url: {
        treeList: window._CONFIG['domianURL'] + '/goodsclassify/cnGoodsClassify/findPageList',
        list: window._CONFIG['domianURL'] + '/price/findPageList',
        detail: window._CONFIG['domianURL'] + '/price/getInfo',
        delete: window._CONFIG['domianURL'] + '/price/deleteById',
        startOrStopUrl: window._CONFIG['domianURL'] + '/price/startOrStop',
        deleteBatch: '/test/cn-formDemo/deleteBatch',
        exportXlsUrl: '/test/cn-formDemo/exportXls'
      }
    }
  },
  methods: {
    handleButon(btn, data) {
      // 调用新增组件
      if (btn == 'newAdd') {
        this.modaltitle = '产品层级新增'
        this.$refs.popupModal.edit()
      } else if (btn == 'edit') {
        this.modaltitle = '产品层级编辑'
        this.$refs.popupModal.edit()
      } else if (btn == 'detail') {
        this.modaltitle = '产品层级详情'
        this.$refs.popupModal.edit()
      }

    },
    selectNode(e,e2){
      console.log(e)
      console.log(e2)
    },
    onExpand(leafKey){
      console.log(leafKey)
    },
    // 加载树数据
    loadTreeData(arg) {
      if (!this.url.treeList) {
        this.$message.info('请配置树状接口地址')
      }
      postAction(this.url.treeList).then(res => {
        console.log(res.result)
       this.treeData = [...res.result]
      })
    },
    onSearch() {
      console.log('889')
    },
    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ':colsettings'
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues
      const cols = this.defColumns.filter(item => {
        if (item.key == 'rowIndex' || item.dataIndex == 'action') {
          return true
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      this.columns = cols
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ':colsettings'
      let colSettings = Vue.ls.get(key)
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = []
        this.defColumns.forEach(function(item, i, array) {
          allSettingColumns.push(item.dataIndex)
        })
        this.settingColumns = allSettingColumns
        this.columns = this.defColumns
      } else {
        this.settingColumns = colSettings
        const cols = this.defColumns.filter(item => {
          if (item.key == 'rowIndex' || item.dataIndex == 'action') {
            return true
          }
          if (colSettings.includes(item.dataIndex)) {
            return true
          }
          return false
        })
        this.columns = cols
      }
    }
  },
  created() {
    this.initColumns(),
      this.loadTreeData()
  },
  computed: {
    // 新增页面初始化数据
    addModalData() {
      return Object.assign({}, {
        title: this.modaltitle,
        width: '70%',
        config: this.addForm,
        urlAdd: this.urlAdd
      })
    }
  }
}
</script>

<style scoped lang="less">
@import '~@assets/less/common.less';

.totalView {
  display: flex;
  flex-direction: row;

  .left {
    padding: 0 8px 0 0;
    min-width:300px;


    .left-header {
      display: flex;
      justify-content: space-around;

      .left-header-text {
        width: 100%;
        text-align: center;
        font-size: 16px;
        height: 30px;
        line-height: 30px;
        margin-bottom: 26px;
        //background-color: @primary-color;
      }
    }

  }


  .right {
    flex: 1;

  }
}

</style>